<?php
    
    include_once("core/functions.php");

    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
    }
    else if($_SERVER["REQUEST_METHOD"] == "GET")
    {
        if(isset($_GET["formSource"]))
        {
            $source = $_GET["formSource"];
            switch($source)
            {
                case 'cAccount':
                    $id = ($_GET["person"]=="null" ? null : $_GET["person"]);
                    if($id!=null)
                    {
                        $data = Core::getUserDetailsArray($id);
                        $accountType = $data["accountType"];
                        $firstName = $data["firstName"];
                        $lastName = $data["lastName"];
                        $email = $data["email"];
                        $skills = $data["skills"];
                        $staffId = $data["universityId"];
                        $gender = $data["gender"];
                        $contactNumber = $data["contactNumber"];
                        $address = $data["address"];
                        $memberSince = $data["dateCreated"];
                        $activated = ($data["activated"] ? "true" : "false");
                        $json = '{"accountType":"'.$accountType.'","firstName":"'.$firstName.'","lastName":"'.$lastName.'",
                        "email":"'.$email.'","skills":"'.$skills.'","staffId":"'.$staffId.'","gender":"'.$gender.'","contactNumber":"'.$contactNumber.'","address":"'.$address.'","datecreated":"'.$memberSince.'","activated":"'.$activated.'"}';
                    }
                    else
                    {
                        $json = '{"accountType":"","firstName":"","lastName":"","email":"","staffId":"","skills":"","gender":"","contactNumber":"","address":"","datecreated":"","activated":""}';
                    }

                    echo $json;
                    break;
                case 'cEvent':
                    $id = $_GET["event"];
                    if($id!=null)
                    {
                        $data = Core::getEventDetailsArray($id);
                        $name = $data["name"];
                        $description = $data["description"];
                        $startTime = $data["startTime"];
                        $endTime = $data["endTime"];
                        $startDate = $data["startDate"];
                        $endDate = $data["endDate"];
                        $locationDetails = $data["locationDetails"];
                        $carPoolLocation = $data["carPoolLocation"];
                        $carPoolTime = $data["carPoolTime"];
                        $meetingLocation = $data["meetingLocation"];
                        $meetingTime = $data["meetingTime"];
                        $m1 = null;
                        $m2 = null;
                        $m3 = null;
                        $m4 = null;
                        $m5 = null;
                        $mgrs = Core::getEventManagers($id);
                        for($i=0;$i<count($mgrs);$i++)
                        {
                            switch($i)
                            {
                                case 0:
                                    $m1 = $mgrs[$i]["person"];
                                    break;
                                case 1:
                                    $m2 = $mgrs[$i]["person"];
                                    break;
                                case 2:
                                    $m3 = $mgrs[$i]["person"];
                                    break;
                                case 3:
                                    $m4 = $mgrs[$i]["person"];
                                    break;
                                case 4:
                                    $m5 = $mgrs[$i]["person"];
                                    break;
                            }
                        }

                        $json = '{"eventName":"'.$name.'","eventDescription":"'.$description.'","eventStartTime":"'.$startTime.'","eventEndTime":"'.$endTime.'","eventStartDate":"'.$startDate.'","eventEndDate":"'.$endDate.'","locationDetails":"'.$locationDetails.'","meetingLocation":"'.$meetingLocation.'","meetingTime":"'.$meetingTime.'","carPoolLocation":"'.$carPoolLocation.'","carPoolTime":"'.$carPoolTime.'","manager1":"'.$m1.'","manager2":"'.$m2.'","manager3":"'.$m3.'","manager4":"'.$m4.'","manager5":"'.$m5.'"}';
                    }
                    else
                    {
                        $json = '{"eventName":"","eventDescription":"","eventStartTime":"","eventEndTime":"","eventStartDate":"","eventEndDate":"","locationDetails":"","meetingLocation":"","meetingTime":"","carPoolLocation":"","carPoolTime":"","manager1":"","manager2":"","manager3":"","manager4":"","manager5":""}';
                    }

                    echo $json;
                    break;
                case 'cActivity':
                    $id = ($_GET["activity"]=="null" ? null : $_GET["activity"]);
                    if($id!=null)
                    {
                        $data = Core::getActvityDetailsArray($id);
                        $title = $data["title"];
                        $description = $data["description"];
                        $startTime = $data["startTime"];
                        $endTime = $data["endTime"];
                        $startDate = $data["startDate"];
                        $endDate = $data["endDate"];
                        $event = $data["event"];
                        $json = '{"activityTitle":"'.$title.'","activityDescription":"'.$description.'","activityStartTime":"'.$startTime.'","activityEndTime":"'.$endTime.'","activityStartDate":"'.$startDate.'","activityEndDate":"'.$endDate.'","activityEvent":"'.$event.'"}';
                    }
                    else
                    {
                        $json = '{"activityTitle":"","activityDescription":"","activityStartTime":"","activityEndTime":"","activityStartDate":"","activityEndDate":"","activityEvent":""}';
                    }

                    echo $json;
                    break;
                case 'cRole':
                    $id = ($_GET["role"] == "null" ? null : $_GET["role"]);   

                    if($id!=null)
                    {
                        $data  = Core::getRoleDetailsArray($id);
                        $title = $data["title"];
                        $description = $data["description"];
                        $startTime = $data["startTime"];
                        $endTime = $data["endTime"];
                        $startDate = $data["startDate"];
                        $endDate = $data["endDate"];
                        $activity = $data["activity"];
                        $payRate = $data["payRate"];
                        $positions = $data["positions"];
                        $skills = $data["skills"];
                        $json = '{"roleTitle":"'.$title.'","roleDescription":"'.$description.'","roleStartTime":"'.$startTime.'","roleEndTime":"'.$endTime.'","roleStartDate":"'.$startDate.'","roleEndDate":"'.$endDate.'","payRate":"'.$payRate.'","skills":"'.$skills.'","roleActivity":"'.$activity.'","positions":"'.$positions.'"}';
                    }
                    else
                    {
                        $json = '{"roleTitle":"","roleDescription":"","roleStartTime":"","roleEndTime":"","roleStartDate":"","roleEndDate":"","payRate":"","skills":"","roleActivity":""}';
                    }
                    echo $json;
                    break;
                case 'cUniversity':
                    $id = ($_GET["person"] == "null" ? null : $_GET["person"]);
                    if($id != null)
                    {
                        $data = Core::getUniDetailsArray($id);
                        $universityId = $data["universityId"];
                        $degreeName = $data["degreeName"];
                        $major = $data["majors"];
                        $yearOfDegree = $data["yearOfDegree"];
                        $entryToUts = $data["entryToUTS"];
                        $highSchool = $data["highSchool"];
                        $eca = $data["extraCurricularActivities"];
                        $scholarships = $data["scholarships"];
                        $prevInternships = $data["previousInternships"];
                        $internshipOrganised = (($data["internshipOrganised"] == null) || ($data["internshipOrganised"] == 0) ? "false" : "true");
                        $json = '{"universityId":"'.$universityId.'","degreeName":"'.$degreeName.'","major":"'.$major.'","year":"'.$yearOfDegree.'","entry":"'.$entryToUts.'","highSchool":"'.$highSchool.'","extraCurricular":"'.$eca.'","scholarships":"'.$scholarships.'","internship":"'.$prevInternships.'","internOrganised":"'.$internshipOrganised.'"}';
                    }
                    else
                    {
                        $json = '{"universityId":"","degreeName":"","major":"","year":"","entry":"","highSchool":"","extraCurricular":"","scholarships":"","internship":"","internOrganised":"false"}';
                    }
                    echo $json;
                    break;
                case 'cOther' :
                        $id = ($_GET["person"] == "null" ? null : $_GET["person"]);

                        if($id!=null)
                        {
                            $data = Core::getOtherDetailsArray($id);
                            $availability = $data["availability"];
                            $workshops = $data["workshops"];
                            $licence = (($data["driversLicence"] == null) || ($data["driversLicence"] == 0) ? "false" : "true");
                            $refugee= (($data["refugee"] == null) || ($data["refugee"] == 0) ? "false" : "true");
                            $indigenous = (($data["indigenous"] == null) || ($data["indigenous"] == 0) ? "false" : "true");
                            $contract = $data["contractExpiration"];
                            $paperwork = $data["paperworkStatus"];
                            $offer = (($data["offer"] == null) || ($data["offer"] == 0) ? "false" : "true");
                            $json = '{"availability":"'.$availability.'","workshops":"'.$workshops.'","licence":"'.$licence.'","refugee":"'.$refugee.'","indigenous":"'.$indigenous.'","contract":"'.$contract.'","paperwork":"'.$paperwork.'","offer":"'.$offer.'"}';
                        }
                        else
                        {
                            $json = '{"availability":"","workshops":"","licence":"","refugee":"","indigenous":"","contract":"","paperwork":"","offer":""}';
                        }
                        echo $json;
                    break;
                case 'cApplicant':
                    $id = ($_GET["person"]=="null" ? null : $_GET["person"]);
                    //$roleid = ($_GET["applicantRole"]=="null" ? null : $_GET["applicantRole"]);
                    if($id!=null)
                    {
                        $data = Core::getUserDetailsArray($id);
                        $accountType = $data["accountType"];
                        $fullName = $data["firstName"].' '.$data["lastName"];
                        $email = $data["email"];
                        $skills = $data["skills"];
                        $avaliability = $data["availability"];
                        $json = '{"accountType":"'.$accountType.'","fullName":"'.$fullName.'",
                        "email":"'.$email.'","skills":"'.$skills.'","avaliability":"'.$avaliability.'"}';
                    }
                    else
                    {
                        $json = '{"accountType":"","fullName":"","email":"","skills":"","avaliability":""}';
                    }
                    echo $json;
                    break;
                case 'cApplied':
                    $id = ($_GET["person"]=="null" ? null : $_GET["person"]);
                    if($id!=null)
                    {
                        $data = Core::getAppliedByPerson($id);
                        $json = "";
                        for($i=0;$i<count($data);$i++)
                        {
                            $json .= '{';
                            $json .= '"id":"'.$data[$i]['role'].'",';
                            $json .= '"name":"'.$data[$i]['title'].'"';
                            if($i == (count($data)-1))
                            {
                                $json .= '}';
                            }
                            else
                            {
                                $json .= '},';
                            }
                        }
                    }
                    else
                    {
                        $json = '{"id":"","name":""}';
                    }
                    echo $json;
                    break;
            }
            return;
        }
        if(isset($_GET["positions"])){
            $id = ($_GET["positions"] == "null" ? null : $_GET["positions"]);
            if($id != null){
                $data  = Core::getRolePositionArray($id);
                $positions = $data["positions"];
                $payRate = $data["payRate"];
                $json = '{"positions":"'.$positions.'","payRate":"'.$payRate.'"}';
            }
            else{
                $json = '{"positions":"0","payRate":"0.00"}';
            }
        }
        if(isset($_GET["resendActivation"])){
            $id = ($_GET["resendActivation"] == "0" ? null : $_GET["resendActivation"]);
            if($id != null){
                $page = new vwallController($id);
                Core::resendMail($page);
            }
            return;
        }
        if(isset($_GET["staffId"]))
        {
            $personId = Core::sanitize($_GET["staffId"]);
            if($personId != null){
                $data  = Person::hasId($personId);
                if($data["universityId"]!=null)
                {
                    $json = "true";
                }
                else
                {
                    $json = "false";
                }
            }
            else
            {
                $json = 'false';
            }
        }
        if(isset($_GET["managers"])){
            $managers = Core::sanitize($_GET["managers"]);
            if($managers=="all"){
                $data = Person::getPersonsByType(ACC_EVTMGR,ACC_SPROUT);
                $json = "";
                for($i=0;$i<count($data);$i++)
                {
                    $json .= '{';
                    $json .= '"name":"'.$data[$i]['firstName'].' '.$data[$i]['lastName'].'",';
                    $json .= '"id":"'.$data[$i]['personId'].'"';
                    if($i == (count($data)-1))
                    {
                        $json .= '}';
                    }
                    else
                    {
                        $json .= '},';
                    }   
                }
            }
        }
        if(isset($_GET["event"])){
            $event = Core::sanitize($_GET["event"]);
            if($event=="all"){
                $data = Event::getEventByDate("0000-00-00");
                $json = "";
                for($i=0;$i<count($data);$i++)
                {
                    $json .= '{';
                    $json .= '"name":"'.$data[$i]["name"].'",';
                    $json .= '"id":"'.$data[$i]["eventId"].'"';
                    if($i == (count($data)-1))
                    {
                        $json .= '}';
                    }
                    else
                    {
                        $json .= '},';
                    }   
                }
            }
        }
        if(isset($_GET["activityEvent"])){
            $event = Core::sanitize($_GET["activityEvent"]);
            $data = Activity::getActivityByEventId($event);
            $json = "";
            for($i=0;$i<count($data);$i++)
            {
                $json .= '{';
                $json .= '"name":"'.$data[$i]["title"].'",';
                $json .= '"id":"'.$data[$i]["activityId"].'"';
                if($i == (count($data)-1))
                {
                    $json .= '}';
                }
                else
                {
                    $json .= '},';
                }   
            }

        }
        if(isset($_GET["activity"])){
            $activity = Core::sanitize($_GET["activity"]);
            if($activity=="all"){
                $data = Activity::getActivitiesByDate("0000-00-00");
                $json = "";
                for($i=0;$i<count($data);$i++)
                {
                    $json .= '{';
                    $json .= '"name":"'.$data[$i]["title"].'",';
                    $json .= '"id":"'.$data[$i]["activityId"].'"';
                    if($i == (count($data)-1))
                    {
                        $json .= '}';
                    }
                    else
                    {
                        $json .= '},';
                    }   
                }
            }
        }
        if(isset($_GET["roleActivity"])){
            $activity = Core::sanitize($_GET["roleActivity"]);
            $data = Role::getRoleByActivity($activity);
            $json = "";
            for($i=0;$i<count($data);$i++)
            {
                $json .= '{';
                $json .= '"name":"'.$data[$i]["title"].'",';
                $json .= '"id":"'.$data[$i]["roleId"].'"';
                if($i == (count($data)-1))
                {
                    $json .= '}';
                }
                else
                {
                    $json .= '},';
                } 
            }
        }
        if(isset($_GET["role"])){
            $role = Core::sanitize($_GET["role"]);
            if($role=="all"){
                $data = Role::getRolesByDate("0000-00-00");
                $json = "";
                for($i=0;$i<count($data);$i++)
                {
                    $json .= '{';
                    $json .= '"name":"'.$data[$i]["title"].'",';
                    $json .= '"id":"'.$data[$i]["roleId"].'"';
                    if($i == (count($data)-1))
                    {
                        $json .= '}';
                    }
                    else
                    {
                        $json .= '},';
                    }   
                }
            }
        }
        if(isset($_GET["personRole"])){
            $role = Core::sanitize($_GET["personRole"]);
            $data = Role::getPersonByRole($role);
            $json = "";
            for($i=0;$i<count($data);$i++)
            {
                $fullName = Person::getPersonNameById($data[$i]["person"]);
                $json .= '{';
                $json .= '"name":"'.$fullName.'",';
                $json .= '"id":"'.$data[$i]["person"].'"';
                if($i == (count($data)-1))
                {
                    $json .= '}';
                }
                else
                {
                    $json .= '},';
                }
            }
        }
        if(isset($_GET["person"])){
            $person = Core::sanitize($_GET["person"]);
            $json = "";
            if($person=="all"){
                $data = Person::getAllPersonIdName();
                for($i=0;$i<count($data);$i++)
                {
                    $json .= '{';
                    $json .= '"name":"'.$data[$i]['firstName'].' '.$data[$i]['lastName'].'",';
                    $json .= '"id":"'.$data[$i]["personId"].'"';
                    if($i == (count($data)-1))
                    {
                        $json .= '}';
                    }
                    else
                    {
                        $json .= '},';
                    }   
                }
            }
            else {
                $data = Person::getPersonNameById($person);
                if($data!=null)
                {
                    $json .= '{';
                    $json .= '"name":"'.$data.'",';
                    $json .= '"id":"'.$person.'"';
                    $json .= '}';
                }
            }
        }
        if(isset($_GET["appliedRole"])){
            $personId = Core::sanitize($_GET["appliedPerson"]);
            $roleId = Core::sanitize($_GET["appliedRole"]);
            if($personId != null && $roleId != null){
                $data  = Core::getApplied($personId,$roleId);
                $hours = $data["hoursWorked"];
                $attended = (($data["attended"] == null) || ($data["attended"] == 0) ? "false" : "true");
                $accepted = $data["accepted"];
                $json = '{"attended":"'.$attended.'","hours":"'.$hours.'","accepted":"'.$accepted.'"}';
            }
            else{
                $json = '{"attended":"","hours":"","accepted":""}';
            }
        }
        if(isset($_GET["filter"])){
            $filter = Core::sanitize($_GET["filter"]);
            $roleId = Core::sanitize($_GET["role"]);

            $persons = Core::filterApplicants($roleId,$filter);
            $data = Person::getAllPersonIdName();
            $json = "";
            for($i=0;$i<count($persons);$i++)
            {
                $json .= '{';
                $json .= '"name":"'.$persons[$i]['firstName'].' '.$persons[$i]['lastName'].'",';
                $json .= '"id":"'.$persons[$i]["personId"].'"';
                if($i == (count($persons)-1))
                {
                    $json .= '}';
                }
                else
                {
                    $json .= '},';
                }   
            }
        }
        if(isset($_GET["volunteer"])){
            $personId = Core::sanitize($_GET["volunteer"]);
            $data = Person::generateReport($personId);
            $person = Person::getPersonNameById($personId);
            $json = '{"name":"'.$person.'",';
            $json .= '"applied":[';
            $i=0;
            $count = count($data);
            foreach($data as $applied)
            {
                if(is_array($applied))
                {
                    $json.='{';
                    $length = count($applied);
                    $x = 0;
                    foreach($applied as $key=>$value)//loop thorugh each data returned
                    {
                        if($x==$length-1)//check if it is the last iteration
                        {
                            $json.='"'.$key.'"'.':'.'"'.$value.'"';//last iteration no commas
                        }
                        else
                        {
                            $json.='"'.$key.'"'.':'.'"'.$value.'",';//not last iteration add commas
                        }
                        $x++;
                    }
                }
                if($i==$count-1)
                {
                    $json.='}';//end of an activity
                }
                else
                {
                    $json.='},';//more activities
                }                    
                $i++;
            }
            $json .= ']}';
        }
        if(isset($_GET["report"]))
        {
            $eventId = Core::sanitize($_GET["report"]);
            $data = Event::getEventById($eventId);
            $json = '{';//start of json
            $json .= '"event":{';//start of event
            $i=0;
            $len = count($data);
            foreach($data as $key=>$value)
            {
                if($i==$len-1)
                {
                    $json.='"'.$key.'"'.':'.'"'.$value.'"';//last iteration no commas
                }
                else
                {
                    $json.='"'.$key.'"'.':'.'"'.$value.'",';//not last iteration add commas
                }
                $i++;
            }
            /**
               CHECK IF THERE ARE ANY ACTIVITIES FIRST
             */
            if(Activity::getActivitiesNum($eventId) >= 1)
            {
                $activities = Activity::getActivityByEventId($eventId,true);
                $json .= ',"activities":[';//start of activities array
                $j = 0;
                $size = count($activities);
                foreach($activities as $array)//loop through each activities
                {
                    
                    if(is_array($array))
                    {
                        $json.='{';//an activity
                        $length = count($array);
                        $x = 0;
                        foreach($array as $key=>$value)//loop thorugh each data returned
                        {
                            if($x==$length-1)//check if it is the last iteration
                            {
                                $json.='"'.$key.'"'.':'.'"'.$value.'"';//last iteration no commas
                            }
                            else
                            {
                                $json.='"'.$key.'"'.':'.'"'.$value.'",';//not last iteration add commas
                            }
                            $x++;
                        }
                        /**
                        CHECK IF THERE ARE ANY ROLES FIRST
                     */
                        $activityId = $array['activityId'];//get the current activity id
                        if(Role::getRolesNum($activityId) >= 0)
                        {
                            $roles = Role::getRoleByActivity($activityId,true);//get roles array for a specific activity
                            if(!is_null($roles))
                            {
                                $json .= ',"roles":[';//start of roles array
                                $rCount = count($roles);
                                $itr = 0;
                                foreach($roles as $arr)
                                {
                                    $json .= '{';//start of a role
                                    $l = count($arr);
                                    $r = 0;
                                    foreach ($arr as $key => $value) 
                                    {
                                        if(!is_null($array))
                                        {
                                            if($r==$l-1)
                                            {
                                                $json.='"'.$key.'"'.':'.'"'.$value.'"';//last iteration no commas
                                            }
                                            else
                                            {
                                                $json.='"'.$key.'"'.':'.'"'.$value.'",';//not last iteration add commas
                                            }
                                        }
                                        $r++;
                                    }
                                    /**
                                      CHECK IF THERE ARE ANY APPLICANTS
                                     */
                                    $roleId = $arr["roleId"];
                                    if(Role::getAppliedNum($roleId) >= 1)
                                    {

                                        $applied = Role::getAcceptedByRole($roleId,true);
                                        
                                        if(!is_null($applied))
                                        {
                                            $json .= ',"applied":[';//start of applied array
                                            $ln = count($applied);
                                            $lc = 0;
                                            foreach($applied as $app)
                                            {
                                                $json .= '{';//start of a applied data
                                                $ac = count($app);
                                                $al = 0;

                                                foreach($app as $key=>$value)
                                                {
                                                    if(!is_null($app))
                                                    {
                                                        if($al==$ac-1)
                                                        {
                                                            $json.='"'.$key.'"'.':'.'"'.$value.'"';//last iteration no commas
                                                        }
                                                        else
                                                        {
                                                            $json.='"'.$key.'"'.':'.'"'.$value.'",';//not last iteration add commas
                                                        }
                                                    }
                                                    $al++;
                                                }

                                                if($lc==$ln-1)
                                                {
                                                     $json .= '}';//end of applied data
                                                }
                                                else
                                                {
                                                    $json .= '},';//more applied data
                                                }

                                                $lc++;
                                            }
                                            $json .= ']';//end of applied array
                                        }
                                    }

                                    if($itr==$rCount-1)
                                    {
                                        $json .= '}';//end of role
                                    }
                                    else
                                    {
                                        $json .= '},';//more roles
                                    }
                                        
                                    $itr++;
                                }
                                 $json .= ']';//end of roles array
                            }
                        }
                        if($j==$size-1)
                        {
                             $json.='}';//end of an activity
                        }
                        else
                        {
                             $json.='},';//more activities
                        }
                    }
                    
                    $j++;

                }
                $json .= ']';//end of activities array
            }
            $json .= '}';//end of event
            $json .= '}';//end of json

        }
        echo $json;
    }


?>